﻿

Public MustInherit Class SelectionControle(Of TElementSelection,
                                              TWCFClient As {IDisposable, Class, New},
                                              TParametresSuppression As Class,
                                              TCriteresRecherche As Class)
    Inherits System.Web.UI.UserControl

    Public ReadOnly Property ResultatsSelectionnes As List(Of TElementSelection)
        Get
            Dim liste As New List(Of TElementSelection)

            For Each row As GridViewRow In GridViewResultats.Rows
                If CType(row.Cells(0).Controls(0), CheckBox).Checked Then liste.Add(Tools.GetRowDataItem(Of TElementSelection, List(Of TElementSelection))(Me.GridViewResultats, row))
            Next

            Return liste
        End Get
    End Property
    Public Property AutoriserSelectionMultiple As Boolean
        Get
            Return CBool(ViewState("autoriser_selection_multiple"))
        End Get
        Private Set(value As Boolean)
            ViewState("autoriser_selection_multiple") = value
        End Set
    End Property
    Protected MustOverride ReadOnly Property LibelleElement As String
    Protected MustOverride ReadOnly Property LibelleFeminin As Boolean
    Protected MustOverride ReadOnly Property LibelleApostrophe As Boolean
    Protected MustOverride ReadOnly Property UrlFicheElement As String
    Protected MustOverride ReadOnly Property EntiteParametrageType As Integer
    Protected MustOverride ReadOnly Property GridViewResultats As Odyssee.WebControles.AcroGridView
    Protected MustOverride ReadOnly Property ComboType As DropDownList
    Protected MustOverride ReadOnly Property BoutonRechercher As Button
    Protected MustOverride ReadOnly Property DivRecherche As HtmlGenericControl
    Protected MustOverride ReadOnly Property DivGridView As HtmlGenericControl
    Protected MustOverride ReadOnly Property FirstControlFocused As Control


    Protected Overrides Sub OnInit(e As EventArgs)
        Dim appPath As String = Request.ApplicationPath
        If appPath = "/" Then
            appPath = ""
        End If

        OpenDelibPage.AddJS(Me.Page, "selectioncontrole", appPath & "/Scripts/selection.js")

        MyBase.OnInit(e)
    End Sub

    Public Sub Initialiser(afficherCasesSelection As Boolean, afficherBoutons As Boolean, Optional autoriserSelectionMultiple As Boolean = True)
        RemoveHandler Me.BoutonRechercher.Click, AddressOf btnRechercher_Click
        AddHandler Me.BoutonRechercher.Click, AddressOf btnRechercher_Click
        RemoveHandler Me.GridViewResultats.RowCommand, AddressOf gvResultats_RowCommand
        AddHandler Me.GridViewResultats.RowCommand, AddressOf gvResultats_RowCommand
        RemoveHandler Me.GridViewResultats.DataBound, AddressOf gvResultat_DataBound
        AddHandler Me.GridViewResultats.DataBound, AddressOf gvResultat_DataBound

        Me.BoutonRechercher.OnClientClick = "return validResearch('" & Me.DivRecherche.ClientID & "');"

        Me.GridViewResultats.Columns(0).Visible = afficherCasesSelection
        Me.AutoriserSelectionMultiple = autoriserSelectionMultiple
        Me.GridViewResultats.Columns(Me.GridViewResultats.Columns.Count - 1).Visible = afficherBoutons

        If Not IsPostBack Then
            Me.DivGridView.Visible = False

            ' Chargement des types
            Dim result As String = Tools.LoadParametersCombo(ComboType, "types d" & If(LibelleApostrophe, "'", "e ") & LibelleElement, EntiteParametrageType, , , New ListItem("Tous", ""))
            If result <> "" Then
                Tools.NotifyErreur(Me.Page, result)
                Return
            End If

            Me.Page.SetFocus(Me.FirstControlFocused)
        End If
    End Sub

    Private Sub btnRechercher_Click(sender As Object, e As EventArgs)
        ' Lancement de la recherche
        Rechercher()
    End Sub

    Private Sub gvResultats_RowCommand(sender As Object, e As GridViewCommandEventArgs)
        Dim cle As Object = e.CommandArgument

        If cle Is Nothing Then Return

        Select Case e.CommandName.ToUpper
            Case "MODIFIER"
                Response.Redirect(Me.UrlFicheElement & "?modeFiche=1&cle=" & cle.ToString)

            Case "SUPPRIMER"
                Using svc As New TWCFClient
                    Dim params As TParametresSuppression = InitialiserParametresSuppression(cle)
                    If params Is Nothing Then Return

                    Try
                        Dim ok As Boolean = False
                        Dim msgErreur As String = ""
                        Supprimer(svc, params, ok, msgErreur)
                        If ok Then
                            Tools.NotifySuccess(Me.Page, "Suppression effectuée avec succès.")
                            Rechercher()
                        Else
                            Tools.NotifyErreur(Me.Page, msgErreur)
                        End If
                    Catch ex As Exception
                        Tools.NotifyErreur(Me.Page, ex.Message)
                    End Try
                End Using
        End Select
    End Sub
    Private Sub gvResultat_DataBound(sender As Object, e As EventArgs)
        Me.GridViewResultats.HeaderRow.Cells(0).Controls(0).Visible = Me.AutoriserSelectionMultiple
    End Sub

    Public Sub Rechercher()
        Me.DivGridView.Visible = False

        Using svc As New TWCFClient
            Dim criteres As TCriteresRecherche = InitialiserCriteresRecherche()
            If criteres Is Nothing Then Return

            Try
                Dim ok As Boolean = False
                Dim msgErreur As String = ""
                Dim liste As List(Of TElementSelection) = Rechercher(svc, criteres, ok, msgErreur)

                If liste Is Nothing OrElse Not ok Then
                    Tools.NotifyErreur(Me.Page, "Impossible de d'effectuer la recherche d" & If(LibelleApostrophe, "'", "e ") & LibelleElement & "s" & If(msgErreur = "", ".", " :" & ControlChars.NewLine & msgErreur))
                Else
                    Me.DivGridView.Visible = True
                    Me.GridViewResultats.DataSource = liste
                    Me.GridViewResultats.DataBind()
                End If
            Catch ex As Exception
                Tools.NotifyErreur(Me.Page, ex.Message)
            End Try
        End Using
    End Sub

    Protected MustOverride Function InitialiserParametresSuppression(cle As Object) As TParametresSuppression
    Protected MustOverride Sub Supprimer(svc As TWCFClient, params As TParametresSuppression, ByRef ok As Boolean, ByRef messageErreur As String)

    Protected MustOverride Function InitialiserCriteresRecherche() As TCriteresRecherche
    Protected MustOverride Function Rechercher(svc As TWCFClient, criteres As TCriteresRecherche, ByRef ok As Boolean, ByRef messageErreur As String) As List(Of TElementSelection)

End Class
